package com.bdqn.ch02.SZ.sz3;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.net.MalformedURLException;
import java.util.Iterator;

/**
 * DOM4JTest
 *
 * 使用DOM4J 查询手机品牌信息
 *
 * @aurhor Administrator  whs
 * @since 2024/7/1
 */
public class DOM4JSelectTest {
    public static void main(String[] args) {
//                设置编码格式
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");

        Document doc = null;
        //加载DOM树
        SAXReader saxReader = new SAXReader();
        try {
            doc = saxReader.read(new File("src/com/bdqn/ch02/SZ/sz1/手机品牌.xml"));
        } catch (
                DocumentException e) {
            e.printStackTrace();
        } catch (
                MalformedURLException e) {
            e.printStackTrace();
        }

        // 获取 XML 的根节点
        Element root = doc.getRootElement();
            //遍历所有的 Brand 节点
            for (Iterator iterator = root.elementIterator(); iterator.hasNext(); ) {
                Element brandEle = (Element) iterator.next();
                //输出 Brand 标签的 name属性的值
                System.out.println("品牌：" + brandEle.attributeValue("name"));
                // 遍历 Type 标签
                for (Iterator type = brandEle.elementIterator(); type.hasNext(); ) {

                    Element typeEle = (Element) type.next();
                    //输出标签的name属性
                    System.out.println("\t型号：" + typeEle.attributeValue("name"));
                }
            }
        /**
         * 品牌：华为
         * 	型号：P90
         * 品牌：苹果
         * 	型号：Iphone Z
         * 	型号：Iphone ZL
         * 	型号：Iphone 11
         * 	型号：Iphone 12
         * 	型号：Iphone 13
         * 	型号：Iphone 14
         * 	型号：Iphone 15
         * 品牌：SAMSUNG
         * 	型号：NoteX
         */
    }
}
